class Solution {
public:
    using TIII=tuple<int ,int, int>;
    const int nxt[4][2]={{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
    int trapRainWater(vector<vector<int>>& heightMap) {
        int m=heightMap.size(), n=heightMap[0].size();
        priority_queue<TIII, vector<TIII>, greater<>> heap;
        for(int i=0; i<m; i++)
            for(int j=0; j<n; j++)
                if(i==0||i==m-1||j==0||j==n-1) { //边界点
                    heap.push({heightMap[i][j], i, j});
                    heightMap[i][j]=-1;
                }
        
        int ans=0;
        while(!heap.empty())
        {
            auto [minHeight, x, y]=heap.top(); heap.pop();
            for(auto &[dx, dy]:nxt) {
                int nx=x+dx, ny=y+dy;
                if(0<=nx&&nx<m&&0<=ny&&ny<n&&heightMap[nx][ny]>=0) {//[x,y]没有处理过
                    ans+=max(minHeight-heightMap[nx][ny], 0); //如果当前这个点与最小高度点有差值，则可以接水
                    heap.push({max(minHeight, heightMap[nx][ny]), nx, ny});
                    heightMap[nx][ny]=-1; //已访问
                }
            }
        }
        return ans;
    }
};